package com.demo.core.io;

import com.demo.core.Constants;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class TxtWriter {

    OutputStreamWriter osw = null;

    boolean firstWrap = false;

    public TxtWriter(String outfile, boolean isAppend, boolean firstWrap)
            throws IOException {
        this.firstWrap = firstWrap;
        osw = new OutputStreamWriter(new FileOutputStream(outfile, isAppend),
                Constants.ENCODE_UTF8);
    }

    public TxtWriter(String outfile, boolean isAppend) throws IOException {
        osw = new OutputStreamWriter(new FileOutputStream(outfile, isAppend),
                Constants.ENCODE_UTF8);
    }

    public TxtWriter(String outfile) throws IOException {
        this(outfile, false);
    }

    public void write(String str) throws IOException {
        if (firstWrap)
            osw.write(Constants.LINE_SEPERATOR);
        osw.write(str);
        if (!firstWrap)
            osw.write(Constants.LINE_SEPERATOR);
    }

    public void flush() throws IOException {
        osw.flush();
    }

    public void close() throws IOException {
        osw.flush();
        osw.close();
    }
}
